/**
 * AES加密类
 * 
 * 无简介
 * @author xzqiaochu(www.xzzhangqiaochu.top)  
 * @version v1.0.0.200314_beta
 */

/**
 * AES加密函数-用于信息传输
 *
 * 对AES加密的封装
 * @param {string} text 待加密文本（若含有中文，请先进行URL编码）
 * @param {string} key AES密钥
 * @return {string} 加密后文本
 */
function encrypt(text, key) {
    key = CryptoJS.enc.Utf8.parse(key);
    iv_str = randHex(16);
    iv = CryptoJS.enc.Utf8.parse(iv_str);
    var encrypt = CryptoJS.AES.encrypt(text, key, {
        iv: iv,
        mode: CryptoJS.mode.CBC,
        padding: CryptoJS.pad.Pkcs7
    });
    text_encrypt = encrypt.toString();
    iv_with_text_encrypt = iv_str + text_encrypt;
    return iv_with_text_encrypt;
}

/**
 * AES解密函数-用于信息传输
 *
 * 对AES解密的封装
 * @param {string} iv_with_text_encrypt 待解密文本
 * @param {string} key AES密钥
 * @return {string} 解密后文本
 */
function decrypt(iv_with_text_encrypt, key) {
    text_encrypt = iv_with_text_encrypt.substr(16);
    key = CryptoJS.enc.Utf8.parse(key);
    iv = CryptoJS.enc.Utf8.parse(iv_with_text_encrypt.substr(0, 16));
    var decrypt = CryptoJS.AES.decrypt(text_encrypt, key, {
        iv: iv,
        mode: CryptoJS.mode.CBC,
        padding: CryptoJS.pad.Pkcs7
    });
    text = decrypt.toString(CryptoJS.enc.Utf8);
    return text;
}
